
package com.android.finaltracer.engine;

import java.util.ArrayList;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.android.finaltracer.channel.ChannelProxy;
import com.android.finaltracer.gps.Gps;
import com.android.finaltracer.model.CommandInfo;
import com.android.finaltracer.model.DataFormatBasic;
import com.android.finaltracer.model.DataFormatInterface;
import com.android.finaltracer.model.FTDataProvider.RelationColumns;
import com.android.finaltracer.model.FTProviderInterface;
import com.android.finaltracer.model.PositionInfo;
import com.android.finaltracer.model.Setting;
import com.android.finaltracer.model.SettingCreator;
import com.android.finaltracer.model.SyncInfo;
import com.android.finaltracer.model.UserBindInfo;
import com.android.finaltracer.usermanage.BindPhone;
import com.android.finaltracer.usermanage.FtTimer;
import com.android.finaltracer.usermanage.GpsTracerPhone;
import com.android.finaltracer.usermanage.Phone;
import com.android.finaltracer.usermanage.PhoneManager;
import com.android.finaltracer.util.FTKey;

public class EngineService extends Service implements EventNotify,
        Handler.Callback {

    private final static String TAG = "EngineService";
    private TelephonyManager mPhone;
    private ChannelProxy mChannelProxy;
    private Gps mGps;
    private Setting mSettings;
    private PhoneManager mPm = null;
    private Sync mSync;

    public static final int MSG_NOTHING = 0;
    public static final int MSG_UI_SERVICE_STATR_CONTROL = MSG_NOTHING + 1;
    public static final int MSG_UI_SERVICE_NEGATIVE_CONTROL = MSG_UI_SERVICE_STATR_CONTROL + 1;
    public static final int MSG_UI_SERVICE_VERIFY_SUCCESS = MSG_UI_SERVICE_NEGATIVE_CONTROL + 1;
    public static final int MSG_STATR_GPS = MSG_UI_SERVICE_VERIFY_SUCCESS + 1;
    public static final int MSG_STOP_GPS = MSG_STATR_GPS + 1;
    public static final int MSG_UI_SERVICE_BIND_PHONE_REQ = MSG_STOP_GPS + 1;
    public static final int MSG_UI_SERVICE_BIND_PHONE_CNF = MSG_UI_SERVICE_BIND_PHONE_REQ + 1;
    public static final int MSG_UI_SERVICE_BIND_PHONE_RSP = MSG_UI_SERVICE_BIND_PHONE_CNF + 1;
    boolean mAllowRebind = true;

    public static final int MODULE_NOTHING = DataFormatBasic.TYPE_NOTHING;
    public static final int MODULE_BIND = DataFormatBasic.TYPE_BIND_INFO;
    public static final int MODULE_GPS_TRACER = DataFormatBasic.TYPE_COMMAND_INFO;
    public static final int MODULE_SYNC_TRACER = DataFormatBasic.TYPE_SYNC_INFO;

    /*
     * Bind service Area
     */
    final Messenger mMessager = new Messenger(new FTHandler());

    class FTHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_UI_SERVICE_STATR_CONTROL: {
                    String from = (String) msg.obj;
                    uiTraceStartShake(from);
                    break;
                }
                case MSG_UI_SERVICE_NEGATIVE_CONTROL: {
                    String from = (String) msg.obj;
                    mPm.removeSlavePhone(from);
                    break;
                }
                case MSG_UI_SERVICE_VERIFY_SUCCESS: {
                    String from = (String) msg.obj;
                    uiServiceVerifySuccess(from);
                    break;
                }
                    // case MSG_STATR_GPS: {
                    // Log.e(TAG, "start GPS");
                    // String phoneNum = (String) msg.getData().getString(
                    // FTKey.KEY_PHONE_NUMBER);
                    // uiStartTrace(phoneNum);
                    // }
                    // break;
                case MSG_STOP_GPS: {
                    String phoneNum = (String) msg.getData().getString(
                            FTKey.KEY_PHONE_NUMBER);
                    Log.e(TAG, "stop GPS phoneNum = " + phoneNum);
                    if (phoneNum != null) {
                        uiStopTrace(phoneNum);
                    } else {
                        uiStopAllTrace();
                    }
                    break;
                }
                case MSG_UI_SERVICE_BIND_PHONE_REQ:
                case MSG_UI_SERVICE_BIND_PHONE_CNF:
                case MSG_UI_SERVICE_BIND_PHONE_RSP:
                    uiBindPhoneProcess((UserBindInfo) msg.obj);
                    break;
                default:
                    super.handleMessage(msg);
            }
        }
    }

    /*
     * Service method
     */
    @Override
    public void onCreate() {
        Log.i(TAG, "onCreate");
        super.onCreate();
        mPhone = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        // EventNotify.class.
        mChannelProxy = ChannelProxy.getInstance(this, this);
        mSettings = SettingCreator.settingInit(this);
        mGps = Gps.getInstance(this, this);
        mPm = PhoneManager.getInstance();
        FtTimer.initInstance(this);
        mSync = Sync.getInstance(this, this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand - Intent: " + intent);

        boolean notifyHostflag = false;
        /* FIXME should be in oncreat */
        if (!simVerify()) {
            Log.i(TAG, "Sim changed");
            notifyHostflag = true;
        }

        if (notifyHostflag) {
            Log.i(TAG, "**********Sms need sent************");
            // notifyHostflag = false;
        }
        // if (true) {
        if (notifyHostflag) {
            ArrayList<String> relationNumbers = FTProviderInterface
                    .getInstance(this).getAllRelationNumber(
                            RelationColumns.WEIGHT_TYPE + ">'"
                                    + RelationColumns.WEIGHT_TYPE_COMMEN + "'");
            if (relationNumbers != null && relationNumbers.size() > 0) {
                for (int i = 0; i < relationNumbers.size(); i++) {
                    mChannelProxy.send(relationNumbers.get(i), new CommandInfo(
                            CommandInfo.COMMAND_NOTIFY_SIM_CHANGED),
                            ChannelProxy.CHANNEL_SMS);
                }

            }
        }

        return START_REDELIVER_INTENT;
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "onBind");

        return mMessager.getBinder();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        /* FIXME: need to invoked to prevent leakage. */
        return mAllowRebind;
    }

    @Override
    public void onDestroy() {
        Log.i(TAG, "onDestroy");

        mPm.onClear();
        mPm = null;

        super.onDestroy();
    }

    /*
     * sim verify
     */
    private boolean simVerify() {

        Setting m_Setting = new Setting(this);

        String OldImsi = m_Setting.getOldImsi();
        String imsi = mPhone.getSubscriberId();

        Log.i(TAG, "imsi = " + imsi);
        Log.i(TAG, "OldImsi = " + OldImsi);

        if (OldImsi.isEmpty() && (imsi != null) && !imsi.isEmpty()) {
            m_Setting.setOldImsi(imsi);
        }

        ArrayList<String> relationNumbers = FTProviderInterface.getInstance(
                this).getAllRelationNumber(
                RelationColumns.WEIGHT_TYPE + ">'"
                        + RelationColumns.WEIGHT_TYPE_COMMEN + "'");

        if (relationNumbers == null || relationNumbers.size() == 0) {
            return true;
        }

        if ((OldImsi != null) && (OldImsi.isEmpty()) || !(OldImsi.equals(imsi))) {
            return false;
        }

        return true;
    }

    /*
     * Bind Area
     */
    private void uiBindPhoneProcess(UserBindInfo userBindInfo) {
        BindPhone bindPhone;
        boolean remove = false;
        if (userBindInfo.getCommand() == UserBindInfo.REMOTE_BIND_PHONE_REQ) {
            bindPhone = mPm.addBindPhone(userBindInfo.getToPhoneNum(), this,
                    this);
        } else {
            bindPhone = mPm.getSpecifyBindPhone(userBindInfo.getToPhoneNum());
        }
        if (bindPhone != null) {
            remove = bindPhone.uiBindPhoneProcess(userBindInfo);
        }
        if (remove) {
            mPm.removeBindPhone(userBindInfo.getToPhoneNum());
        }
    }

    private void remoteBindPhoneProcess(String from, UserBindInfo userBindInfo) {
        BindPhone bindPhone;
        boolean remove = false;
        if (userBindInfo.getCommand() == UserBindInfo.REMOTE_BIND_PHONE_REQ) {
            bindPhone = mPm.addBindPhone(from, this, this);
        } else {
            bindPhone = mPm.getSpecifyBindPhone(from);
        }
        if (bindPhone != null) {
            remove = bindPhone.remoteBindPhoneProcess(from, userBindInfo);
        }
        if (remove) {
            mPm.removeBindPhone(userBindInfo.getToPhoneNum());
        }
    }

    /*
     * Command Area
     */
    public void associatedPhoneCommandProcess(String from, int command) {
        Log.i(TAG, "associatedPhoneCommand:" + command);
        GpsTracerPhone gpsTracerPhone = null;
        ArrayList<GpsTracerPhone> phoneList;
        boolean remove = false;
        boolean isSlaveCommand = false;
        if (command == CommandInfo.COMMAND_NOTIFY_SIM_CHANGED) {
            gpsTracerPhone = mPm.addSlavePhone(from, this, this);
        } else if (command == CommandInfo.COMMAND_HANDSHAKE_REQ) {
            gpsTracerPhone = mPm.addMasterPhone(from, this, this);
        }
        if (command == CommandInfo.COMMAND_NOTIFY_SIM_CHANGED
                || command == CommandInfo.COMMAND_HANDSHAKE_CNF) {
            isSlaveCommand = true;
        }
        if (isSlaveCommand) {
            phoneList = mPm.getSlavePhoneList();
        } else {
            phoneList = mPm.getMasterPhoneList();
        }
        if (gpsTracerPhone == null) {
            for (GpsTracerPhone phone : phoneList) {
                if (phone.isSamePhone(from)) {
                    gpsTracerPhone = phone;
                    break;
                }
            }
        }
        if (gpsTracerPhone != null) {
            gpsTracerPhone.clearRetryCount();
            remove = gpsTracerPhone
                    .associatedPhoneCommandProcess(from, command);
        }
        if (remove && isSlaveCommand) {
            mPm.removeSlavePhone(from);
        } else if (remove) {
            mPm.removeMasterPhone(from);
        }
    }

    public void associatedPhoneGpsPositionChanged(String from,
            PositionInfo position) {
        Log.i(TAG, "Show map changed : " + position.toString());
        ArrayList<GpsTracerPhone> slavePhoneList;
        slavePhoneList = mPm.getSlavePhoneList();
        for (GpsTracerPhone phone : slavePhoneList) {
            if (phone.isSamePhone(from)) {
                phone.associatedPhoneGpsPositionChanged(position);
            }
        }
    }

    /*
     * Command Area ui start
     */
    private void uiTraceStartShake(String phoneNum) {
        Log.i(TAG, "COMMAND_START_HANDSHAKE_REQ");
        // GpsTracerPhone slaveGpsTracerPhone =
        // mPm.getSpecifySlavePhone(phoneNum);
        // 如果是远程手机上报simchange 那么因为早就add过 直接get没有问题
        // 当是UI直接手动启动控制 需要add
        // add逻辑会自动检查是否已经add过 如果add过就直接返回 没有就会生成新的
        GpsTracerPhone slaveGpsTracerPhone = mPm.addSlavePhone(phoneNum, this,
                this);
        slaveGpsTracerPhone.clearRetryCount();
        slaveGpsTracerPhone.startTraceShake();
    }

    private void uiStopTrace(String phoneNum) {
        Log.e(TAG, "uiStopTrace " + phoneNum);
        GpsTracerPhone slaverGpsTracerPhone = mPm
                .getSpecifySlavePhone(phoneNum);
        if (slaverGpsTracerPhone != null) {
            slaverGpsTracerPhone.stopTrace();
        }
        mPm.removeSlavePhone(phoneNum);
    }

    private void uiStopAllTrace() {
        ArrayList<GpsTracerPhone> slavePhoneList = mPm.getSlavePhoneList();
        ArrayList<String> phoneNums = null;
        for (GpsTracerPhone phone : slavePhoneList) {
            if (phone != null
                    && phone.getPhoneStatus() == GpsTracerPhone.PHONE_STATUS_WAIT_GPS_INFO) {
                phone.stopTrace();
                if (phoneNums == null) {
                    phoneNums = new ArrayList<String>();
                }
                phoneNums.add(phone.getPhoneNum());
            }
        }
        if (phoneNums != null && phoneNums.size() > 0) {
            for (String phoneNum : phoneNums) {
                mPm.removeSlavePhone(phoneNum);
            }
        }
    }

    private void uiServiceVerifySuccess(String phoneNum) {
        Log.i(TAG, "COMMAND_SEND_VERIFY_SUCCESS");
        GpsTracerPhone slaveGpsTracerPhone = mPm.getSpecifySlavePhone(phoneNum);
        slaveGpsTracerPhone.verifySuccess();
        mPm.removeSlavePhone(phoneNum);
    }

    /*
     * Sync Area
     */
    private void startSync(String toPhoneNum, int module) {
        Log.i(TAG, "SEVICE SYNC_REQ");
        mSync.startSync(toPhoneNum, module);
    }

    /*
     * Sync Area
     */
    public void remoteSyncInfo(String from, SyncInfo si) {
        mSync.remoteCnfSync(from, si);
    }

    /*
     * Sync Area Expire
     */
    /* FIXME */
    private void onBindExpire(int timerId, String phoneNum) {
        // timerId can be TIMER_ID_BIND_WAIT_BIND_CNF
        // TIMER_ID_BIND_WAIT_BIND_RSP
        int phoneStatus = Phone.PHONE_STATUS_NULL;
        BindPhone bindPhone = mPm.getSpecifyBindPhone(phoneNum);
        if (bindPhone != null) {
            phoneStatus = bindPhone.getPhoneStatus();
        }
        switch (phoneStatus) {
            case BindPhone.PHONE_STATUS_WAIT_BIND_CNF:
                if (timerId == FtTimer.TIMER_ID_BIND_WAIT_BIND_CNF) {
                    if (bindPhone.isOutRetry()) {
                        FTProviderInterface.getInstance(this).clearWaitBindSign(
                                phoneNum);
                        mPm.removeBindPhone(phoneNum);
                        // FIXME to be add tip bind error
                    } else {
                        bindPhone.uiBindPhoneProcess(new UserBindInfo(
                                UserBindInfo.REMOTE_BIND_PHONE_REQ, phoneNum,
                                SettingCreator.settingInit(this).getImeiStr()));
                    }
                }
                // mPm.removeBindPhone(phoneNum);
                // popUi(phoneNum, null);
                break;
            case BindPhone.PHONE_STATUS_WAIT_BIND_RSP:
                if (timerId == FtTimer.TIMER_ID_BIND_WAIT_BIND_RSP) {
                    if (bindPhone.isOutRetry()) {
                        FTProviderInterface.getInstance(this).clearWaitBindSign(
                                phoneNum);
                        mPm.removeBindPhone(phoneNum);
                        // FIXME to be add tip bind error
                    } else {
                        bindPhone.uiBindPhoneProcess(new UserBindInfo(
                                UserBindInfo.REMOTE_BIND_PHONE_CNF, phoneNum,
                                SettingCreator.settingInit(this).getImeiStr(),
                                true, true));
                    }
                }
                // mPm.removeBindPhone(phoneNum);
                // popUi(phoneNum, null);
                break;
            case BindPhone.PHONE_STATUS_NULL:
                // do nothing
                break;
            default:
                break;
        }
    }

    /*
     * GpsTracer Area Expire
     */
    /* FIXME */
    private void onGpsTracerExpire(int timerId, String phoneNum) {
        int phoneStatus = Phone.PHONE_STATUS_NULL;
        GpsTracerPhone phone = null;
        if (timerId == FtTimer.TIMER_ID_COMMAND_WAIT_HANDSHANKE_CNF
                || timerId == FtTimer.TIMER_ID_COMMAND_WAIT_GPS_INFO) {
            phone = mPm.getSpecifySlavePhone(phoneNum);
        } else if (timerId == FtTimer.TIMER_ID_COMMAND_WAIT_MASTER_ORDER
                || timerId == FtTimer.TIMER_ID_COMMAND_WAIT_PERIOD_CHECK) {
            phone = mPm.getSpecifyMasterPhone(phoneNum);
        }
        if (phone != null) {
            phoneStatus = phone.getPhoneStatus();
        }
        switch (phoneStatus) {
            // master
            case GpsTracerPhone.PHONE_STATUS_WIAT_HANDSHANKE_CNF:
                if (timerId == FtTimer.TIMER_ID_COMMAND_WAIT_HANDSHANKE_CNF) {
                    // need sync do it again
                    if (!phone.isOutRetry()) {
                        phone.startTraceShake();
                    } else {
                        mPm.removeSlavePhone(phoneNum);
                        // FIXME to be add notify ui
                    }
                } else if (timerId == FtTimer.TIMER_ID_COMMAND_WAIT_GPS_INFO) {
                    // do nothing
                }
                break;
            case GpsTracerPhone.PHONE_STATUS_WAIT_GPS_INFO:
                if (timerId == FtTimer.TIMER_ID_COMMAND_WAIT_HANDSHANKE_CNF) {
                    // do nothing
                } else if (timerId == FtTimer.TIMER_ID_COMMAND_WAIT_GPS_INFO) {
                    // need sync do it again
                    // startSync(phoneNum, MODULE_GPS_TRACER);
                    if (mSync.isSlaveAlive()) {
                        FtTimer.startTimer(EngineService.MODULE_GPS_TRACER,
                                FtTimer.TIMER_ID_COMMAND_WAIT_GPS_INFO, phoneNum,
                                FtTimer.DEFAULT_LONG_DELAY);
                        mSync.setSlaveAlive(phoneNum, false);
                    } else {
                        mPm.removeSlavePhone(phoneNum);
                        mSync.setSlaveAlive(phoneNum, false);
                        // FIXME to be add notify ui
                    }
                }
                break;
            // slave
            case GpsTracerPhone.PHONE_STATUS_HANDSHANKE_CONNECTED:
                if (timerId == FtTimer.TIMER_ID_COMMAND_WAIT_MASTER_ORDER) {
                    // need sync do it again
                    if (!phone.isOutRetry()) {
                        phone.associatedPhoneCommandProcess(phoneNum,
                                CommandInfo.COMMAND_HANDSHAKE_REQ);
                        // 用下面的本来也是可以 但是会多一个逻辑重新遍历phone
                        // 并且，需要注意的是下面的逻辑会清楚retrycount
                        // 不可以使用
                        // associatedPhoneCommandProcess(phoneNum,
                        // CommandInfo.COMMAND_HANDSHAKE_REQ);
                    } else {
                        mPm.removeSlavePhone(phoneNum);
                        // FIXME to be add notify ui
                    }
                } else if (timerId == FtTimer.TIMER_ID_COMMAND_WAIT_PERIOD_CHECK) {
                    // do nothing
                }

                break;
            case GpsTracerPhone.PHONE_STATUS_SEND_GPS_INFO:
                if (timerId == FtTimer.TIMER_ID_COMMAND_WAIT_MASTER_ORDER) {
                    // do nothing
                } else if (timerId == FtTimer.TIMER_ID_COMMAND_WAIT_PERIOD_CHECK) {
                    // need sync if can not sync stop gps
                    // mPm.removeSlavePhone(phoneNum);
                    startSync(phoneNum, MODULE_GPS_TRACER);
                }
                break;
            // case Phone.PHONE_STATUS_GPS_STOPED:
            // if (timerId == FtTimer.TIMER_ID_COMMAND_WAIT_MASTER_ORDER) {
            // //need sync
            // }else if(timerId == FtTimer.TIMER_ID_COMMAND_WAIT_PERIOD_CHECK){
            // //need sync if can not sync stop gps
            // mPm.removeSlavePhone(phoneNum);
            // }
            // break;
            case Phone.PHONE_STATUS_NULL:
                if (timerId == FtTimer.TIMER_ID_COMMAND_WAIT_HANDSHANKE_CNF) {
                    // do nothing
                } else if (timerId == FtTimer.TIMER_ID_COMMAND_WAIT_GPS_INFO) {
                    // do nothing
                } else if (timerId == FtTimer.TIMER_ID_COMMAND_WAIT_MASTER_ORDER) {
                    // do nothing
                } else if (timerId == FtTimer.TIMER_ID_COMMAND_WAIT_PERIOD_CHECK) {
                    // do nothing
                }
                break;
            default:
                break;
        }
    }

    /*
     * Sync Area Expire 现在同步只用于 被控制端周期检查master是否alive 其他没有
     */
    /* FIXME */
    private void onSyncExpire(int timerId, String phoneNum) {
        if (mSync.isOutRetry()) {
            associatedPhoneCommandProcess(phoneNum,
                    CommandInfo.COMMAND_STOP_MONITOR_GPS_POSITION);
        } else {
            mSync.onFailer(phoneNum);
        }
    }

    /* FIXME */
    private void onRemoteCnfGpsTracerSync(String from, int remotePhoneStatus,
            boolean isAlive) {
        int phoneStatus = Phone.PHONE_STATUS_NULL;
        Phone phone = null;
        // String mySelfPhoneNum=mSettings.getSelfNumber();
        if (remotePhoneStatus == GpsTracerPhone.PHONE_STATUS_WIAT_HANDSHANKE_CNF
                || remotePhoneStatus == GpsTracerPhone.PHONE_STATUS_WAIT_GPS_INFO) {
            // phone = mPm.getSpecifyMasterPhone(mySelfPhoneNum);
            phone = mPm.getSpecifyMasterPhone(from);
            if (phone != null) {
                phoneStatus = phone.getPhoneStatus();
            }
            if (phoneStatus == GpsTracerPhone.PHONE_STATUS_SEND_GPS_INFO
                    && remotePhoneStatus == GpsTracerPhone.PHONE_STATUS_WAIT_GPS_INFO) {
                FtTimer.startTimer(EngineService.MODULE_GPS_TRACER,
                        FtTimer.TIMER_ID_COMMAND_WAIT_PERIOD_CHECK, from,
                        FtTimer.DEFAULT_LONG_DELAY);
            } else {
                mGps.stop();
                mPm.removeMasterPhone(from);
            }
        }

        // else if (remotePhoneStatus ==
        // GpsTracerPhone.PHONE_STATUS_HANDSHANKE_CONNECTED
        // || remotePhoneStatus == GpsTracerPhone.PHONE_STATUS_SEND_GPS_INFO
        // || remotePhoneStatus == GpsTracerPhone.PHONE_STATUS_GPS_STOPED) {
        // phone = mPm.getSpecifySlavePhone(mySelfPhoneNum);
        // if (phone != null) {
        // phoneStatus = phone.getPhoneStatus();
        // }
        // if(phoneStatus==GpsTracerPhone.PHONE_STATUS_WAIT_GPS_INFO&&remotePhoneStatus
        // == GpsTracerPhone.PHONE_STATUS_SEND_GPS_INFO){
        // FtTimer.startTimer(MODULE_GPS_TRACER,
        // FtTimer.TIMER_ID_COMMAND_WAIT_GPS_INFO, from);
        // }else{
        // uiStopTrace(from);
        // }
        // }
    }

    /*
     * interface Area for FtTimer
     */
    @Override
    public boolean handleMessage(Message msg) {
        // TODO Auto-generated method stub
        switch (msg.what) {
            case MODULE_BIND:
                onBindExpire(msg.arg1, (String) msg.obj);
                break;
            case MODULE_GPS_TRACER:
                onGpsTracerExpire(msg.arg1, (String) msg.obj);
                break;
            case MODULE_SYNC_TRACER:
                onSyncExpire(msg.arg1, (String) msg.obj);
                break;
            default:
                break;
        }
        return false;
    }

    /*
     * interface Area for eventnotify
     */
    @Override
    public void localGpsPositionChanged(PositionInfo position) {
        ArrayList<GpsTracerPhone> masterPhoneList;
        masterPhoneList = mPm.getMasterPhoneList();
        for (GpsTracerPhone phone : masterPhoneList) {
            phone.localGpsPositionChanged(position);
        }
    }

    @Override
    public void receive(String from, DataFormatInterface dfb) {
        // TODO Auto-generated method stub
        // DataFormatBasic dataFormatBasic = DataFormatBasic.deCode(str);
        switch (dfb.getType()) {
            case DataFormatBasic.TYPE_POSITION_INFO:
                associatedPhoneGpsPositionChanged(from, ((PositionInfo) dfb));
                break;
            case DataFormatBasic.TYPE_COMMAND_INFO:
                associatedPhoneCommandProcess(from,
                        ((CommandInfo) dfb).getCommand());
                break;
            case DataFormatBasic.TYPE_BIND_INFO:
                remoteBindPhoneProcess(from, ((UserBindInfo) dfb));
                break;
            case DataFormatBasic.TYPE_SYNC_INFO:
                remoteSyncInfo(from, (SyncInfo) dfb);
                break;
            default:
                break;
        }
    }

    // @Override
    // public void notifyNetworkState(boolean isNetworkOk) {
    // // TODO Auto-generated method stub
    //
    // }

    @Override
    public int getSpecifyPhoneStatus(String from, int module) {
        // TODO Auto-generated method stub
        int state = Phone.PHONE_STATUS_NULL;
        switch (module) {
            case MODULE_BIND:
                state = mPm.getSpecifyBindPhone(from) != null ? mPm
                        .getSpecifyBindPhone(from).getPhoneStatus()
                        : Phone.PHONE_STATUS_NULL;
                break;
            case MODULE_GPS_TRACER:
                state = mPm.getSpecifyMasterPhone(from) != null ? mPm
                        .getSpecifyMasterPhone(from).getPhoneStatus()
                        : Phone.PHONE_STATUS_NULL;
                if (state == Phone.PHONE_STATUS_NULL) {
                    state = mPm.getSpecifySlavePhone(from) != null ? mPm
                            .getSpecifySlavePhone(from).getPhoneStatus()
                            : Phone.PHONE_STATUS_NULL;
                }
                break;
            default:
                break;
        }
        return state;
    }

    @Override
    public void onRemoteCnfSync(String from, int module, int phoneStatus,
            boolean isAlive) {
        // TODO Auto-generated method stub
        switch (module) {
            case MODULE_BIND:
                break;
            case MODULE_GPS_TRACER:
                onRemoteCnfGpsTracerSync(from, phoneStatus, isAlive);
                break;
            case MODULE_SYNC_TRACER:
                break;
            default:
                break;
        }

    }
}
